﻿///<reference path="../jquery-1.4.1.min.js" />
///<reference path="../jquery-ui.min.js" />
///<reference path="../jquery.validate.min.j" />
///<reference path="../jquery.validate.rules.js" />

$(function () {
    $('.submit-link').click(function (e) {
        e.preventDefault();
        $(this).parents("form:first").submit();
    })

    $(".save_ShippingAddress").click(function (e) {
        var chooseItem = $(".shipping_address :radio[checked]");

        var binders = $(chooseItem).parents("tr:first").find(".binder");
        binders.each(function (index, item) {
            var name = $(item).attr("data-name");

            if ($(item).is("span")) {

                var value = $.trim($(item).text());
                $("input[name=" + name + "]", ".common-form").val(value);
            } else {
                var value = $.trim($(item).val());
                $("select", ".common-form").val(value);
            }
        });
        syncAddress();
        $(".shipping_address").dialog("close");
    });

    $(".save_paymentAddress").click(function (e) {
        var chooseItem = $(".payment_address :radio[checked]");

        var binders = $(chooseItem).parents("tr:first").find(".binder");
        binders.each(function (index, item) {
            var name = $(item).attr("data-name");

            if ($(item).is("span")) {

                var value = $.trim($(item).text());
                $("input[name=" + name + "]", ".common-form").val(value);
            } else {
                var value = $.trim($(item).val());
                $("select", ".common-form").val(value);
            }
        });
        syncAddress();
        $(".payment_address").dialog("close");
    });
});


$(document).ready(function () {
    //address dialog
    $(".dialog").dialog({ autoOpen: false, height: 300, width: 500, modal: true });

    //open dialog
    $(".changeAddress").click(function () {
        var dialogClass = $(this).attr("data");
        $("." + dialogClass).dialog("open");
    });

    //sync between shipping address and payment address
    $("#payment input[type=text]").each(function (index, obj) {
        var id = $(obj).attr("data-equ");
        $("#" + id).bind("blur", function () {
            if ($("#SameAsShippingAddress").attr("checked")) {
                $(obj).val($(this).val());
            }
        });
    });

    $("#payment select").each(function (index, obj) {
        var id = $(obj).attr("data-equ");
        $("#" + id).bind("blur", function () {
            if ($("#SameAsShippingAddress").attr("checked")) {
                $(obj).val($(this).val());
            }
        });
    });

    //same as shipping 
    $("#SameAsShippingAddress").change(function () {
        var newValue = $(this).attr("checked") ? false : true;
        changePaymentAddressStatus(newValue);
        $("#payment_address_main").slideToggle("slow");
    });

    changePaymentAddressStatus(!$("#SameAsShippingAddress").attr("checked"));
    if ($("#SameAsShippingAddress").attr("checked")) {
        $("#payment_address_main").slideToggle();
    }

    $("#checkout").validate();
});

function changePaymentAddressStatus(isEnable) {
    $("#payment input[type=text]").each(function (index, obj) {
        if (isEnable)
            $(obj).removeAttr("disabled");
        else
            $(obj).attr("disabled", "true");
    });

    $("#payment select").each(function (index, obj) {
        if (isEnable)
            $(obj).removeAttr("disabled");
        else
            $(obj).attr("disabled", "true");
    });

    syncAddress();
}

function syncAddress() {
    if ($("#SameAsShippingAddress").attr("checked")) {
        $("#payment input[type=text]").each(function (index, obj) {
            var id = $(obj).attr("data-equ");
            $(obj).val($("#" + id).val());
            $(obj).removeClass("error");
        });

        $("#payment select").each(function (index, obj) {
            var id = $(obj).attr("data-equ");
            $(obj).val($("#" + id).val());
            $(obj).removeClass("error");
        });

        $("#payment_changeAddress").hide();
    } else {
        $("#payment_changeAddress").show();
    }
}